//
//  MyPaintBoardView.m
//  MyPaintBoard
//
//  Created by 黄珂耀 on 15/8/6.
//  Copyright (c) 2015年 黄珂耀. All rights reserved.
//

#import "MyPaintBoardView.h"

@implementation MyPaintBoardView
{
     BOOL _isAllLine;
    
    NSMutableArray* myAllPoints;
    NSMutableArray* myAllLines;
    NSMutableArray* myAllColors;
    NSMutableArray* myAllLineWidths;
}

#pragma mark - DefaultOpertion

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    return self;
}

#pragma mark - DrawRect

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetLineCap(context, kCGLineCapRound);
    
    CGContextSetLineJoin(context, kCGLineJoinRound);
    
    if (_isAllLine == NO)
    {
        myAllLines = [[NSMutableArray alloc] initWithCapacity:10];
        myAllColors = [[NSMutableArray alloc] initWithCapacity:10];
        myAllLineWidths = [[NSMutableArray alloc] initWithCapacity:10];
        
        _isAllLine = YES;
    }
    if ([myAllLines count] > 0)
    {
        for (int i = 0; i < [myAllLines count]; i++)
        {
            NSArray *tempArray = [NSArray arrayWithArray:[myAllLines objectAtIndex:i]];
            
            if ([myAllColors count] > 0)
            {
                _color = [myAllColors objectAtIndex:i];
                _lineWidth = [[myAllLineWidths objectAtIndex:i] floatValue];
            }
            
            if ([tempArray count] > 1)
            {
                CGContextBeginPath(context);
                
                CGPoint myStartPoint = [[tempArray objectAtIndex:0] CGPointValue];
                
                CGContextMoveToPoint(context, myStartPoint.x, myStartPoint.y);
                
                for (int j = 0; j < [tempArray count] - 1; j++)
                {
                    CGPoint myEndPoint = [[tempArray objectAtIndex:j + 1] CGPointValue];
                    
                    CGContextAddLineToPoint(context, myEndPoint.x, myEndPoint.y);
                }
                
                CGContextSetStrokeColorWithColor(context, _color.CGColor);
                CGContextSetFillColorWithColor(context, _color.CGColor);
                
                CGContextSetLineWidth(context, _lineWidth);
                
                CGContextStrokePath(context);
            }
        }
    }
    
    if ([myAllPoints count] > 1)
    {
        CGContextBeginPath(context);
        
        CGPoint myStartPoint = [[myAllPoints objectAtIndex:0] CGPointValue];
        
        CGContextMoveToPoint(context, myStartPoint.x, myStartPoint.y);
        
        for (int i = 0; i < [myAllPoints count] - 1; i++)
        {
            CGPoint myEndPoint = [[myAllPoints objectAtIndex:i + 1] CGPointValue];
            
            CGContextAddLineToPoint(context, myEndPoint.x, myEndPoint.y);
        }
        
        CGContextSetStrokeColorWithColor(context, [[myAllColors lastObject] CGColor]);
        CGContextSetFillColorWithColor(context, [[myAllColors lastObject] CGColor]);
        
        CGContextSetLineWidth(context, [[myAllLineWidths lastObject] floatValue]);
        
        CGContextStrokePath(context);
    }
}

#pragma mark - Actions

- (void)initWithMyAllPoints
{
    myAllPoints = [[NSMutableArray alloc] initWithCapacity:10];
}

- (void)addLine
{
    [myAllLines addObject:myAllPoints];
}

- (void)setPoint:(CGPoint)sender
{
    NSValue *pointValue = [NSValue valueWithCGPoint:sender];
    [myAllPoints addObject:pointValue];
}

- (void)addColor
{
    [myAllColors addObject:_color];
}

- (void)addLineWidth
{
    NSNumber *lineWidthNumber = [NSNumber numberWithFloat:_lineWidth];
    [myAllLineWidths addObject:lineWidthNumber];
}

- (void)cancelAction
{
    if ([myAllLines count] > 0)
    {
        [myAllLines removeLastObject];
        [myAllColors removeLastObject];
        [myAllLineWidths removeLastObject];
        [myAllPoints removeAllObjects];
    }
    [self setNeedsDisplay];
}

- (void)clearAction
{
    if ([myAllLines count]>0)
    {
        [myAllLines removeAllObjects];
        [myAllColors removeAllObjects];
        [myAllLineWidths removeAllObjects];
        [myAllPoints removeAllObjects];
        myAllLines = [[NSMutableArray alloc] initWithCapacity:10];
        myAllColors = [[NSMutableArray alloc] initWithCapacity:10];
        myAllLineWidths = [[NSMutableArray alloc] initWithCapacity:10];
        [self setNeedsDisplay];
    }
}


@end
